package com.yndsoft.jc.supplier.perfect;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.adapter.PerfectAdapter;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.model.EventModel;
import com.yndsoft.jc.supplier.model.PerfectModel;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.widget.recyclerview.IRecyclerSwipe;
import com.yndsoft.jc.supplier.widget.recyclerview.refresh.OnLoadMoreListener;
import com.yndsoft.jc.supplier.widget.recyclerview.refresh.RecyclerViewWithFooter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class PerfectActivity extends SupplierBaseActivity implements IRecyclerSwipe, SwipeRefreshLayout.OnRefreshListener, OnLoadMoreListener, View.OnClickListener {

    private SwipeRefreshLayout swipeRefresh;
    private RecyclerViewWithFooter recyclerView;
    private int page = 0;
    private final int COUNT = 10;
    private boolean isRefresh = false;
    private PerfectAdapter perfectAdapter;

    @Override
    public int getContentResId() {
        return R.layout.activity_perfect;
    }

    public static void start(Context context) {
        context.startActivity(new Intent(context, PerfectActivity.class));
    }

    @Override
    public void initView() {
        ImageButton barBack = findViewById(R.id.bar_back);
        TextView barTitle = findViewById(R.id.bar_title);
        ImageButton barAdd = findViewById(R.id.bar_add);
        swipeRefresh = findViewById(R.id.swipe_refresh);
        recyclerView = findViewById(R.id.recycler_view);

        barTitle.setText("项目列表");
        barAdd.setVisibility(View.VISIBLE);
        barBack.setOnClickListener(this);
        barAdd.setOnClickListener(this);
        swipeRefresh.setColorSchemeColors(0xFFFFBB33, 0xFFFF4444, 0xFF99CC00, 0xFF33B5E5);
        swipeRefresh.setOnRefreshListener(this);
        recyclerView.setOnLoadMoreListener(this);
    }

    @Override
    public void initData() {
        EventBus.getDefault().register(this);
        loadAdapter();
        loadData(0);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 100)
    public void onEvent(EventModel model) {
        if (model != null && (model.getInteger() == Constants.PERFECT_UPDATE || model.getInteger() == Constants.PERFECT_ADD)) {
            if (perfectAdapter != null) {
                perfectAdapter.clearData();
                loadData(0);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        swipeRefresh.post(new Runnable() {
//            @Override
//            public void run() {
//                swipeRefresh.setRefreshing(true);
//            }
//        });
        onEvent(new EventModel());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void loadAdapter() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        perfectAdapter = new PerfectAdapter(mContext);
        recyclerView.setAdapter(perfectAdapter);
    }

    private void loadData(int page) {
        mHttpUtil.perfectList(new ProgressSubscriber(mContext, this) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        setEmptyData();
                        break;
                    case 1:
                        paging(returnMsg, true);
                        break;
                    case 2:
                        paging(returnMsg, false);
                        break;
                }
            }
        }, "productlist", mSession.getSid(), page, COUNT);
    }

    private void paging(ReturnMsg returnMsg, boolean isPaging) {
        String msg = returnMsg.getInfo();
        if (msg != null) {
            List<PerfectModel> model = new Gson().fromJson(msg, new TypeToken<List<PerfectModel>>() {}.getType());
            if (model != null && model.size() > 0) {
                if (perfectAdapter != null) {
                    if (isRefresh) {
                        perfectAdapter.setData(model);
                    } else {
                        perfectAdapter.addData(model);
                    }
                }
                if (!isPaging) {
                    setNoMore();
                }
            } else {
                setEmptyData();
            }
        }
    }

    private void setEmptyData() {
        if (perfectAdapter != null) {
            perfectAdapter.clearData();
            perfectAdapter.notifyDataSetChanged();
            recyclerView.setEnd("获取数据为空");
        }
    }

    @Override
    public void showItemFail(String msg) {

    }

    @Override
    public void hideSwipeLoading() {
        swipeRefresh.setRefreshing(false);
    }

    @Override
    public void setNoMore() {
        recyclerView.setEnd("没有更多数据");
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 0;
        loadData(page);
    }

    @Override
    public void onLoadMore() {
        isRefresh = false;
        page++;
        loadData(page);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                finish();
                break;
            case R.id.bar_add:
                PerfectAddActivity.start(mContext);
                break;
        }
    }
}
